package com.xbts.web.controller.cloud;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xbts.system.domain.CnOrderItem;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.xbts.common.annotation.Log;
import com.xbts.common.core.controller.BaseController;
import com.xbts.common.core.domain.AjaxResult;
import com.xbts.common.enums.BusinessType;
import com.xbts.system.domain.CnOrder;
import com.xbts.system.service.ICnOrderService;
import com.xbts.common.utils.poi.ExcelUtil;
import com.xbts.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;

/**
 * 订单Controller
 * @author xbts
 * @date 2023-08-03
 */
@Api("订单")
@RestController
@RequestMapping("/cloud/order")
public class CnOrderController extends BaseController {

    @Autowired
    private ICnOrderService cnOrderService;

    /**
     * 查询订单列表
     */
    @ApiOperation("查询订单列表")
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @PostMapping("/list")
    public TableDataInfo list(@RequestBody CnOrder cnOrder)     {
        startPage(cnOrder);
        List<CnOrder> list = cnOrderService.selectCnOrderList(cnOrder);
        return getDataTable(list);
    }

    /**
     * 导出订单列表
     */
    @ApiOperation("导出订单列表")
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "订单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response,@RequestBody CnOrder cnOrder) {
        List<CnOrder> list = cnOrderService.selectCnOrderList(cnOrder);
        ExcelUtil<CnOrder> util = new ExcelUtil<CnOrder>(CnOrder.class);
        util.exportExcel(response, list, "订单数据");
    }

    /**
     * 获取订单详细信息
     */
    @ApiOperation("获取订单详细信息")
    @ApiImplicitParam(name = "orderId", value = "订单ID", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "/{orderId}")
    public AjaxResult getInfo(@PathVariable("orderId") String orderId) {
        return success(cnOrderService.selectCnOrderByOrderId(orderId));
    }
    /**
     * 获取订单详细信息
     */
    @ApiOperation("获取订单详细信息")
    @ApiImplicitParam(name = "sn", value = "订单编号", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @GetMapping(value = "getBySn/{sn}")
    public AjaxResult getBySn(@PathVariable("sn") String sn) {
        return success(cnOrderService.getOne(new LambdaQueryWrapper<CnOrder>()
                .eq(CnOrder::getSn, sn)));
    }

    /**
     * 新增订单
     */
    @ApiOperation("新增订单")
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "订单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody CnOrder cnOrder) {
        return toAjax(cnOrderService.insertCnOrder(cnOrder));
    }

    /**
     * 修改订单
     */
    @ApiOperation("修改订单")
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "订单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody CnOrder cnOrder) {
        return toAjax(cnOrderService.updateCnOrder(cnOrder));
    }

    /**
     * 删除订单
     */
    @ApiOperation("删除订单")
    @ApiImplicitParam(name = "orderIds", value = "订单ID集合", required = true, dataType = "String", paramType = "path", dataTypeClass=String.class)
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "订单", businessType = BusinessType.DELETE)
	@PostMapping("/batchDelete")
    public AjaxResult batchDelete(@RequestBody List<String> orderIds) {
        return toAjax(cnOrderService.deleteCnOrderByOrderIds(orderIds));
    }

    /**
     * 云服务管理-审核
     * @return
     */
    @PostMapping("review")
    public AjaxResult review(@RequestBody CnOrderItem cnOrderItem){
        return cnOrderService.review(cnOrderItem);
    }

}
